//
//  FacebookViewController.swift
//  NDAi
//
//  Created by 丸子 on 2024/6/20.
//

import UIKit

class FeedbookViewController: BaseViewController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.editNav(isCurrent: true)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.editNav(isCurrent: false)
    }
    private lazy var tipsLb: UILabel = {
        let label = UILabel()
        label.text = "您可以在这里简单说一下，您的意见或者遇到的问题，提交后我们会认真阅读，且尽快联系您"
        label.numberOfLines = 2
        label.font = UIFont.pingFangSCRegular(ofSize: 12)
        label.textColor = Theme.color.text_d2
        return label
    }()
    let placeholderText = "请填写您遇到的问题，以便帮助我们做的更好"
    
    private lazy var backView: UIView = {
       let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    }()
    
    private lazy var textView: UITextView = {
         let textView = UITextView()
         textView.delegate = self
         textView.text = placeholderText
         textView.font = UIFont.pingFangSCRegular(ofSize: 14)
         textView.textColor = Theme.color.text_d3
         textView.backgroundColor = .clear
         return textView
     }()
     
    lazy var textField: UITextField = {
        let textField = UITextField()
        textField.borderStyle = .none
        textField.backgroundColor = .white
        textField.textColor = Theme.color.text_d1
        textField.font = UIFont.pingFangSCSemibold(ofSize: 14)
        textField.returnKeyType = .done // 设置键盘完成按钮
        textField.attributedPlaceholder =  NSAttributedString.init(string: "QQ/手机号/邮箱", attributes: [NSAttributedString.Key.font: UIFont.pingFangSCRegular(ofSize: 12),NSAttributedString.Key.foregroundColor:Theme.color.text_d3])
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 12, height: 0))
        textField.leftView = leftView
        textField.leftViewMode = .always
        return textField
    }()
    
    private lazy var submitBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle("提交反馈", for: .normal)
        button.allRound(24)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = UIFont.pingFangSCMedium(ofSize: 16)
        button.backgroundColor = UIColor(hexString: "#25262B")
        return button
    }()
     
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(hexString: "#F3F2F7")
        self.navigationItem.title = "意见反馈"
        
        self.view.addSubviews([tipsLb, backView, textView, textField, submitBtn])
        tipsLb.snp.makeConstraints { make in
            make.top.equalTo(self.safeTop).offset(24)
            make.leading.trailing.equalToSuperview().inset(14)
        }
        
        backView.snp.makeConstraints { make in
            make.top.equalTo(tipsLb.snp.bottom).offset(16)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(200)
        }
        
        textView.snp.makeConstraints { make in
            make.top.bottom.equalTo(backView).inset(8)
            make.leading.trailing.equalTo(backView).inset(12)
        }
        
        textField.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(backView.snp.bottom).offset(10)
            make.height.equalTo(54)
        }
        
        submitBtn.snp.makeConstraints { make in
            make.bottom.equalTo(self.safeBottom).offset(-32)
            make.leading.trailing.equalToSuperview().inset(14)
            make.height.equalTo(48)
        }
        
        submitBtn.setClosureTouchUpInside_k { [weak self] in
            self?.submitContent()
        }
    }
    

    func submitContent(){
        if self.textView.text.count <= 0 && self.textView.text == placeholderText  {
            NDProgressHUD.show(message: "请输入反馈内容")
            return
        }
        if self.textField.text?.count ?? 0 <= 0 {
            NDProgressHUD.show(message: "请输入联系方式")
            return
        }
        NDProgressHUD.showLoading()
        RequestService.instance.feedback(mobile: self.textField.text ?? "", content: self.textView.text, completeClosure: {
            NDProgressHUD.showSuccess(title: "", subtitle: "反馈成功")
            self.navigationController?.popViewController(animated: true)
        }, failureClosure: { err in
            NDProgressHUD.showSuccess(title: "", subtitle: "反馈失败请重试")
        })
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
}
extension FeedbookViewController: UITextViewDelegate {
    func textViewDidBeginEditing(_ textView: UITextView) {
        if textView.textColor == Theme.color.text_d3 {
            textView.text = nil
            textView.textColor = Theme.color.text_d1
        }
    }

    func textViewDidEndEditing(_ textView: UITextView) {
        if textView.text.isEmpty {
            textView.text = placeholderText
            textView.textColor = Theme.color.text_d3
        }
    }
}
